package com.osmino.core.utils.view;

import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.View;
import android.widget.ImageView;

public class Backgroung {
	
	public static class SrcDrawableState{
		private boolean mReversed = false;
		public SrcDrawableState(ImageView v, int picDef, int picAct){
			mView = v;
			mPicDef = picDef;
			mPicAct = picAct;
		}
		private ImageView mView;
		private int mPicDef = -1;
		private int mPicAct = -1;
		
		public void reverse(){
			mReversed = !mReversed;
			if(mReversed)
				setResource(mView, mPicAct, mPicDef);
			else
				setResource(mView, mPicDef, mPicAct);
		}
		
		public boolean isReversed(){
			return mReversed;
		}
	}
	
	public static class BgDrawableState{
		private boolean mReversed = false;
		public BgDrawableState(View v, int picDef, int picAct){
			mView = v;
			mPicDef = picDef;
			mPicAct = picAct;
		}
		private View mView;
		private int mPicDef = -1;
		private int mPicAct = -1;
		
		public void reverse(){
			mReversed = !mReversed;
			if(mReversed)
				setBackground(mView, mPicAct, mPicDef);
			else
				setBackground(mView, mPicDef, mPicAct);
		}
		
		public boolean isReversed(){
			return mReversed;
		}
	}
	
	public static class BgColorState{
		private boolean mReversed = false;
		public BgColorState(View v, int colorDef, int colorAct){
			mView = v;
			mColorDef = colorDef;
			mColorAct = colorAct;
		}
		private View mView;
		private int mColorDef = -1;
		private int mColorAct = -1;
		
		public void reverse(){
			mReversed = !mReversed;
			if(mReversed)
				setBackgroundColor(mView, mColorAct, mColorDef);
			else
				setBackgroundColor(mView, mColorDef, mColorAct);
		}
		
		public boolean isReversed(){
			return mReversed;
		}
	}
	
	public static SrcDrawableState setResource(ImageView v, int picDef, int picAct){
		StateListDrawable states = new StateListDrawable();
		Resources res = v.getResources();
		states.addState(new int[] {android.R.attr.state_pressed}, res.getDrawable(picAct));
		states.addState(new int[] {android.R.attr.state_focused}, res.getDrawable(picAct));
		states.addState(new int[] { }, res.getDrawable(picDef));
		v.setImageDrawable(states);
		return new SrcDrawableState(v, picDef, picAct);
	}
	
	public static BgDrawableState setBackground(View v, int picDef, int picAct){
		StateListDrawable states = new StateListDrawable();
		Resources res = v.getResources();
		states.addState(new int[] {android.R.attr.state_pressed}, res.getDrawable(picAct));
		states.addState(new int[] {android.R.attr.state_focused}, res.getDrawable(picAct));
		states.addState(new int[] { }, res.getDrawable(picDef));
		v.setBackgroundDrawable(states);
		return new BgDrawableState(v, picDef, picAct);
	}
	
	public static BgColorState setBackgroundColor(View v, int colorDef, int colorAct){
		StateListDrawable states = new StateListDrawable();
		Drawable colorActDrawable = new ColorDrawable(colorAct);
		Drawable colorDefDrawable = new ColorDrawable(colorDef);
		states.addState(new int[] {android.R.attr.state_pressed}, colorActDrawable);
		states.addState(new int[] {android.R.attr.state_focused}, colorActDrawable);
		states.addState(new int[] { }, colorDefDrawable);
		v.setBackgroundDrawable(states);
		return new BgColorState(v, colorDef, colorAct);
	}
}
